﻿/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
using System;
using System.Collections.Generic;
using Microsoft.SqlServer.Server;

namespace Xtee.Core.Envelope
{

    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/",Target = typeof(Fault))]
    public class FaultSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected Fault clazz;
        private  IFaultDetailSerializer _faultDetailSerializer;

        public FaultSerializer(Fault clazz)
        {
            this.clazz = clazz; 
        }

        public FaultSerializer Set(IFaultDetailSerializer detailSerializer)
        {
            _faultDetailSerializer = detailSerializer;
            return this;
        }

        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            EnsureDetailSerializer();
            Writer.WriteStartElement("faultcode");
            if (clazz.Faultcode != null)
            {
                Writer.WriteString(new global::Xtee.Core.Schema.XsdQName(((string)(clazz.Faultcode))).ToString());
            }
            else
            {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("faultstring");
            if (clazz.Faultstring != null)
            {
                Writer.WriteCDATA(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Faultstring))).ToString());
            }
            else
            {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("faultactor");
            if (clazz.Faultactor != null)
            {
                Writer.WriteString(new global::Xtee.Core.Schema.XsdAnyUri(((string)(clazz.Faultactor))).ToString());
            }
            else
            {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("detail");
            _faultDetailSerializer.Write(clazz.Detail,Writer); 
            Writer.WriteEndElement();
        }

        private void EnsureDetailSerializer()
        {
            if (_faultDetailSerializer == null)
            {
                throw new ArgumentException("Fault detail serializer expected.");
            }
        }

        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader)
        {
            EnsureDetailSerializer();
            base.ReadXml(Reader);
            while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                if ((Reader.IsElementNil() || Reader.IsEmptyElement))
                {
                    Reader.Skip();
                    continue;
                }

                switch (Reader.LocalName)
                {

                    case "faultcode":
                        clazz.Faultcode = new global::Xtee.Core.Schema.XsdQName(Reader.ReadElementString("faultcode")).Value;
                        break;
                    case "faultstring":
                        clazz.Faultstring = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("faultstring")).Value;
                        break;
                    case "faultactor":
                        clazz.Faultactor = new global::Xtee.Core.Schema.XsdAnyUri(Reader.ReadElementString("faultactor")).Value;
                        break;
                    case "faultdetail":
                        clazz.Detail = _faultDetailSerializer.Read(Reader);
                        break;
                    case "detail":
                        clazz.Detail = _faultDetailSerializer.Read(Reader);
                        break; 
                    default:
                        {
                            Reader.Skip();
                            break;
                        } 

                }

            } 
           
        }
    }
}
